(function () {
    'use strict';
    angular.module('BlurAdmin.theme.components')
        .config(ApiCodeInterceptorConfiguration);

    function ApiCodeInterceptorConfiguration($httpProvider) {

        function ApiCodeInterceptor($window, $injector,$q,cfpLoadingBar) {
            return {
                request: function(config){
                    cfpLoadingBar.start();
                    return config;
                },
                requestError: function(err){
                    cfpLoadingBar.complete();
                    return $q.reject(err);
                },
                response: function(res){
                    cfpLoadingBar.complete();
                    return $q.resolve(res);
                },
                responseError: function(err){
                    cfpLoadingBar.complete();
                    if(-1 === err.status) {
                        console.log("远程服务器无响应");
                    } else if(404 === err.status) {
                        console.log("找不到资源");
                    } else if(401 === err.status) {
                        //$injector.get('$state').go("");//跳转绑定页
                        $injector.get('AppUtils').showAlert("未绑定微信号！");
                    } else if(400 === err.status) {
                        $injector.get('AppUtils').showAlert("缺失必要参数，请刷新页面重试！");
                    } else {
                        console.log("发生错误,代码：" + err.status);
                    }
                    return $q.reject(err);
                }
            }
        }
        $httpProvider.interceptors.push(ApiCodeInterceptor)
    }
})();

